package com.hexacta.hrs.report.dtos;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import com.hexacta.hrs.domain.staffing.assignment.AvailabilityStatus;

/**
 * 
 * @author nscuri
 *
 */
public class ResourcesSummaryDetailsDTO implements Comparable<ResourcesSummaryDetailsDTO> {

	private String selectedDateFrom;
	
	private String selectedDateTo;
	
	private Collection<ResourcesAssignmentDTO> totals;
	
	private Collection<ResourcesAssignmentDTO> assignments;
	
	
	public ResourcesSummaryDetailsDTO() {
		
		ResourcesAssignmentDTO unassignedResources = new ResourcesAssignmentDTO(AvailabilityStatus.NoAsignado.toString());
		ResourcesAssignmentDTO assignedClientResources = new ResourcesAssignmentDTO(AvailabilityStatus.AsignadoCliente.toString());
		ResourcesAssignmentDTO addOnAvailableResources = new ResourcesAssignmentDTO(AvailabilityStatus.AddOnAvailable.toString());
		ResourcesAssignmentDTO addOnNotAvailableResources = new ResourcesAssignmentDTO(AvailabilityStatus.AddOnNotAvailable.toString());
		ResourcesAssignmentDTO InternalResources = new ResourcesAssignmentDTO(AvailabilityStatus.Interno.toString());
		
		this.assignments = new ArrayList<ResourcesAssignmentDTO>();
		this.assignments.add(assignedClientResources);
		this.assignments.add(addOnNotAvailableResources);
		this.assignments.add(addOnAvailableResources);
		this.assignments.add(InternalResources);
		this.assignments.add(unassignedResources);
		
		this.totals = new ArrayList<ResourcesAssignmentDTO>();
	}
	
	@Override
	public int compareTo(ResourcesSummaryDetailsDTO o) {
		return 0;
		//TODO: implementar la logica para determinar cuando dos objetos de la clase son iguales.
	}

	public void setSelectedDateFrom(String selectedDateFrom) {
		this.selectedDateFrom = selectedDateFrom;
	}


	public String getSelectedDateFrom() {
		return selectedDateFrom;
	}


	public void setSelectedDateTo(String selectedDateTo) {
		this.selectedDateTo = selectedDateTo;
	}


	public String getSelectedDateTo() {
		return selectedDateTo;
	}

	public void setAssignments(Collection<ResourcesAssignmentDTO> assignments) {
		this.assignments = assignments;
	}

	public Collection<ResourcesAssignmentDTO> getAssignments() {
		return assignments;
	}
	
	
	public void increment(AvailabilityStatus status, int month, double amount) {
		for (ResourcesAssignmentDTO assignment: this.assignments) {
			if(status.toString().equals(assignment.getAssignmentType())) {
				assignment.increment(month, amount);
				break;
			}
		}
	}

	public void calculatePercentages() {
		
		ResourcesAssignmentDTO totals = this.getTotals().iterator().next();
		for(int index = 0; index < 12; index++){
			double total = totals.getAmount(index);
			for (ResourcesAssignmentDTO assignment: this.assignments) {
				assignment.calculatePercentages(total, index);
			}
		}
	}
	
	public void setMonthAndYear(int month, Date actual) {
		for (ResourcesAssignmentDTO assignment: this.assignments) {
			assignment.setMonthAndYear(month, actual);
		}
	}

	public void setTotals(Collection<ResourcesAssignmentDTO> totals) {
		this.totals = totals;
	}

	public Collection<ResourcesAssignmentDTO> getTotals() {
		return totals;
	}
	
	public void calculateTotals() {
		
		ResourcesAssignmentDTO dto = new ResourcesAssignmentDTO("Total");
		for (ResourcesAssignmentDTO assignment: this.getAssignments()) {
			for(int index = 0; index < 12; index++ ) {
				dto.increment(index, assignment.getAmount(index));
			}
		}
		this.getTotals().add(dto);
	}

	
}